<template>
  <div class="container">
    <div class="top">
      <el-date-picker
        value-format="yyyy-MM-dd HH:mm:ss"
        v-model="search.date"
        type="datetime"
        placeholder="选择日期时间"
      ></el-date-picker>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索商品信息"
        v-model="commodityNameList.name"
      >
        <template slot="prepend">商品</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="commodityBtn"
        ></el-button>
      </el-input>
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索类别信息"
        v-model="classNameList.name"
      >
        <template slot="prepend">类别</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="classBtn"
        ></el-button>
      </el-input>

      <!-- <el-select
        style="margin-bottom: 5px"
        v-model="search.house_code"
        :multiple="false"
        filterable
        remote
        center
        reserve-keyword
        placeholder="请选择仓库"
        :remote-method="entrepolistsMethod"
        :loading="entrepot.loading"
      > -->
      <!-- <el-option
          v-for="item in entrepot.option"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select> -->
      <el-input
        style="width: 300px"
        :disabled="true"
        placeholder="请搜索仓库"
        v-model="warehouseList.name"
      >
        <template slot="prepend">仓库</template>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="warehouseBtn"
        ></el-button>
      </el-input>

      <el-select v-model="search.is_normal" placeholder="请选择商品结算类型">
        <el-option
          v-for="item in businessType"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="search.has_stock" placeholder="">
        <el-option
          v-for="item in has_stockList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      >
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" align="center"></el-table-column> -->
      <el-table-column
        prop="index"
        label="序号"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_id"
        label="商品id"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="erp_goods_code"
        label="商品code"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="class_name"
        label="类别"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="unit"
        label="单位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="warehouse_name"
        label="仓库"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_num"
        label="原始库存"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="inp_money"
        label="原始库存金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="qty"
        label="原始库存箱数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="current_num"
        label="当前剩余数量"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="current_inp_money"
        label="当前剩余金额"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="current_qty"
        label="当前剩余箱数"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="created_at"
        label="最后入库时间"
        align="center"
      ></el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <commodityDialog
      :commoditydialog="commoditydialog"
      @tag="commodityTag"
    ></commodityDialog>
    <classDialog :classdialog="classdialog" @tag="classTag"></classDialog>
    <warehouseselect
      :warehouseselect="warehouseselect"
      @tag="warehouseTag"
    ></warehouseselect>
  </div>
</template>
<script>
import { getnowDate } from "@/utils/index";
import Pagination from "@/components/pagination";
import CommodityDialog from "@/components/grossMargin/commodityDialog";
import ClassDialog from "@/components/grossMargin/classDialog";
import { selectChooseWarehouse } from "@/api/warehouse/warehouse";
import { inventoryList } from "@/api/commodity/commodityList";
import { getToken } from "@/utils/auth";
import Warehouseselect from "@/components/commodity/warehouseselect";
export default {
  components: {
    pagination: Pagination,
    commodityDialog: CommodityDialog,
    classDialog: ClassDialog,
    warehouseselect: Warehouseselect,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      //默认时间
      search: {
        date: "",
        house_code: "",
        is_normal: "",
        has_stock:0
      },
      has_stockList:[
      {
          value: 0,
          label: "全部",
        },
        {
          value: 1,
          label: "当前有库存商品",
        },
      ],
      //dialog信息
      commoditydialog: {
        centerDialogVisible: false,
      },
      classdialog: {
        centerDialogVisible: false,
      },
      warehouseselect: {
        centerDialogVisible: false,
      },
      commodityNameList: {
        name: "",
        code: "",
      },
      classNameList: {
        name: "",
        code: "",
      },
      warehouseList: {
        name: "",
        code: "",
      },
      entrepot: {
        innerVisible: false,
        option: [],
        list: [],
        loading: false,
      },
      businessType: [
        {
          value: -1,
          label: "全部",
        },
        {
          value: 0,
          label: "不结算",
        },
        {
          value: 1,
          label: "正常结算",
        },
        {
          value: 2,
          label: "非正常结算",
        },
      ],
    };
  },

  created() {
    //页面数据
    this.getData();
    // this.getoptionData();
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        date: this.search.date,
        is_normal: this.search.is_normal == -1 ? "" : this.search.is_normal,
        house_code: this.warehouseList.code,
        class_id: this.classNameList.code,
        goods_id: this.commodityNameList.code,
        has_stock:this.search.has_stock,
      };
      inventoryList(data)
        .then((res) => {
          if (res.code == 0) {
            this.tableData = res.data.data;
            this.pageData.page = res.data.current_page;
            this.pageData.pagesize = res.data.per_page;
            this.pageData.total = res.data.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    commodityTag(data) {
      this.commodityNameList = {
        name: "",
        code: "",
      };
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.commodityNameList.name = arrName.join(",");
      this.commodityNameList.code = arrCode.join(",");
    },
    classTag(data) {
      this.classNameList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.id);
      });
      this.classNameList.name = arrName.join(",");
      this.classNameList.code = arrCode.join(",");
    },
    warehouseTag(data) {
      this.warehouseList = {
        name: "",
        code: "",
      };
      console.log(data, 11111);
      let arrName = [];
      let arrCode = [];
      data.forEach((res) => {
        arrName.push(res.name);
        arrCode.push(res.code);
      });
      this.warehouseList.name = arrName.join(",");
      this.warehouseList.code = arrCode.join(",");
    },
    //超级商品搜索
    commodityBtn() {
      this.commoditydialog = {
        centerDialogVisible: true,
      };
    },
    //超级类别搜索
    classBtn() {
      this.classdialog = {
        centerDialogVisible: true,
      };
    },
    //超级仓库搜索
    warehouseBtn() {
      this.warehouseselect = {
        centerDialogVisible: true,
      };
    },
    // 获取仓库方法
    entrepolistsMethod(query) {
      if (query !== "") {
        this.getenterpolistList(query);
      } else {
        this.entrepot.options = [];
      }
    },
    //获取仓库数据
    // getenterpolistList(query) {
    //   this.getoptionData(query);
    // },
    // getoptionData(name = "") {
    //   let data = {
    //     page: 1,
    //     limit: 500,
    //     name: name,
    //   };
    //   //获取仓库
    //   selectChooseWarehouse(data).then((res) => {
    //     if (res.data.data) {
    //       this.entrepot.option = res.data.data.map((item) => {
    //         return { value: `${item.house_code}`, label: `${item.house_name}` };
    //       });
    //     }
    //   });
    // },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },
    //清空
    empty() {
      this.search = {
        date: "",
        house_code: "",
        is_normal: "",
        has_stock:0
      };
      this.entrepot = {
        innerVisible: false,
        option: [],
        list: [],
        loading: false,
      };
      this.classNameList.name = "";
      this.commodityNameList.name = "";
      this.classNameList.code = "";
      this.commodityNameList.code = "";
      this.warehouseList.name = "";
      this.warehouseList.code = "";
      // this.getoptionData()
    },
    //导出
    derive() {
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          let is_normal =
            this.search.is_normal == -1 ? "" : this.search.is_normal;
          link.href =
            this.$axios.defaults.baseURL +
            "/api/Stock/export?token=" +
            getToken() +
            "&date=" +
            this.search.date +
            "&house_code=" +
            this.warehouseList.code +
            "&class_id=" +
            this.classNameList.code +
            "&has_stock="+
            this.search.has_stock +
            "&goods_id=" +
            this.commodityNameList.code +
            "&is_normal=" +
            is_normal +
            "&export=1";
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;
      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;
      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}

.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>